/**
 * Created by ChengYa on 2016/6/18.
 */

//判断手机类型
window.onload = function () {
    var u = navigator.userAgent;
    if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//安卓手机
    } else if (u.indexOf('iPhone') > -1) {//苹果手机
        //屏蔽ios下上下弹性
        $(window).on('scroll.elasticity', function (e) {
            e.preventDefault();
        }).on('touchmove.elasticity', function (e) {
            e.preventDefault();
        });
    } else if (u.indexOf('Windows Phone') > -1) {//winphone手机
    }
    document.addEventListener('UniAppJSBridgeReady', function () {
        console.log("加载完成，可以使用uni相关接口");
        uni.getEnv(function (res) {
            console.log('当前环境：' + JSON.stringify(res));
        });
    });
}

var turnPageSize = 1;
var turnPageIndex = 1;
//加载页面
function loading(pageSize, pageIndex) {
    turnPageSize = pageSize || 1;
    turnPageIndex = pageIndex || 1;
    var tagHtml = "";
    for (var i = 0; i < pageSize; i++) {
        tagHtml += "<div id='turnPageDiv" + i + "'></div>";
    }
    $(".flipbook").append(tagHtml);

    yepnope({
        test: Modernizr.csstransforms,
        yep: ['js/turn.js'],
        both: ['js/zoom.min.js'],
        complete: loadApp
    });
}

//配置turn.js
function loadApp() {
    //var w = $(".graph").width();
    $(".flipbook-viewport").show();
    var w = $(window).width();
    var h = $(window).height();
    $('.flipboox').width(w).height(h);
    $(window).resize(function () {
        w = $(window).width();
        h = $(window).height();
        $('.flipboox').width(w).height(h);
    });
    $('.flipbook').turn({
        width: w,
        height: h,
        page: turnPageIndex,
        acceleration: true,
        elevation: 50,
        display: 'single',
        gradients: true,
        autoCenter: true,
        zoom: 4,
        when: {
            turning: function (e, page, view) {
                $(".btnImg").css("display", "block");
                if (page == 1) {
                    //$(".btnImg").css("display", "none");
                    //$(".mark").css("display", "block");
                    console.info("已到第一页");
                } else {
                    //$(".btnImg").css("display", "block");
                    //$(".mark").css("display", "none");
                }
                if (page == turnPageSize) {
                    $(".nextPage").css("display", "none");
                    console.info("已到最后一页");
                } else {
                    $(".nextPage").css("display", "block");
                }
            },
            turned: function (e, page, view) {
                var index = page - 1;
                if (document.getElementById("turnPageDiv" + index).style.background == "") {
                    document.getElementById("turnPageDiv" + index).style.background = "url(https://www.dazongpai.com/reader/upload/book/image/" + book.bookId + "/" + book.bookId + "_" + index + ".png) center top no-repeat";
                    document.getElementById("turnPageDiv" + index).style.backgroundSize = "100%";
                }

                if (document.getElementById("turnPageDiv" + page).style.background == "") {
                    $.ajax({
                        //请求方式
                        type: "POST",
                        //请求的媒体类型
                        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                        // contentType: "application/json;charset=UTF-8",
                        //请求地址
                        url: "/reader/controller/SelectBookImage?bookId=" + book.bookId + "&bookFile=" + book.bookFile + "&pageIndex=" + page,
                        //数据，json字符串
                        data: JSON.stringify(book),
                        //请求成功
                        success: function (result) {
                            console.log("jquery receive data ===" + result);
                            document.getElementById("turnPageDiv" + page).style.background = "url(https://www.dazongpai.com/reader/upload/book/image/" + book.bookId + "/" + book.bookId + "_" + page + ".png) center top no-repeat";
                            document.getElementById("turnPageDiv" + page).style.backgroundSize = "100%";
                        },
                        //请求失败，包含具体的错误信息
                        error: function (e) {
                            console.log(e.status);
                            console.log(e.responseText);
                        }
                    });
                }

                // var total = $(".flipbook").turn("pages");//总页数
                $(".btnImg").css("display", "block");
                if (page == 1) {
                    //$(".return").css("display", "none");
                    //$(".btnImg").css("display", "none");
                } else {
                    //$(".return").css("display", "block");
                    //$(".btnImg").css("display", "block");
                }

                book.bookPageIndex = page;
                uni.postMessage({
                    data: {
                        action: 'handleMessage',
                        book: book
                    }
                });

                //if (page == 2) {
                //    $(".catalog").css("display", "block");
                //} else {
                //    $(".catalog").css("display", "none");
                //}
            }
        }
    });

    // $('.flipbook').zoom({
    //     flipbook: $('.flipbook'),
    //     max: 3
    // });
}
/**
 $('.flipbook-viewport').zoom({
    flipbook: $('.flipbook'),
    max: function () {
        return largeMagazineWidth() / $('.flipbook').width();
    },
    when: {
        tap: function (event) {
            if ($(this).zoom('value') == 1) {
                $('.flipbook').removeClass('animated').addClass('zoom-in');
                $(this).zoom('zoomIn', event);
            } else {
                $(this).zoom('zoomOut');
            }
        },
        resize: function (event, scale, page, pageElement) {
            if (scale == 1)
                loadSmallPage(page, pageElement);
            else
                loadLargePage(page, pageElement);
        },
        zoomIn: function () {
            // $('.flipbook').hide();
            $('.flipbook').addClass('zoom-in');
            if (!window.escTip && !$.isTouch) {
                escTip = true;
                $('<div />', {'class': 'esc'}).html('<div>Press ESC to exit</div>').appendTo($('body')).delay(2000).animate({opacity: 0}, 500, function () {
                    $(this).remove();
                });
            }
        },
        zoomOut: function () {
            $('.esc').hide();
            // $('.flipbook').fadeIn();
            setTimeout(function () {
                $('.flipbook').addClass('animated').removeClass('zoom-in');
                resizeViewport();
            }, 0);
        },
        swipeLeft: function () {
            $('.flipbook').turn('next');
        },
        swipeRight: function () {
            $('.flipbook').turn('previous');
        }
    }
});
 **/

